package com.bootdo.wx.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.bootdo.common.utils.*;
import com.bootdo.wx.dao.MemberUserDao;
import com.bootdo.wx.dao.OrderTeamMoneyDao;
import com.bootdo.wx.domain.MemberApplyDO;
import com.bootdo.wx.domain.MemberUserDO;
import com.bootdo.wx.service.MemberApplyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

/**
 * 申请升级记录表
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2021-05-16 09:55:48
 */
 
@Controller
@RequestMapping("/wx/memberApply")
public class MemberApplyController {
	@Autowired
	private MemberApplyService memberApplyService;
	@Autowired
	private MemberUserDao memberUserDao;
	@Autowired
	private OrderTeamMoneyDao orderTeamMoneyDao;
	
	@GetMapping()
	@RequiresPermissions("wx:memberApply:memberApply")
	String MemberApply(){
	    return "wx/memberApply/memberApply";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:memberApply:memberApply")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","ca_at");
	    params.put("order","desc");

		Map<String,Object> dateMap = new HashMap<>();
		Date strDate ;
		Date endDate ;
		int i = DateUtil.dayOfMonth(new Date());
		if(i==1){
			//上月2号开始时间
			strDate= DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.beginOfMonth(DateUtil.lastMonth()),1));
			endDate=DateUtil.endOfDay(new Date());
		}else{
			//这个月2号为开始时间
			strDate= DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.beginOfMonth(new Date()),1));
			endDate=DateUtil.endOfDay(new Date());
		}
		dateMap.put("strDate",strDate);
		dateMap.put("endDate",endDate);

        //查询列表数据
        Query query = new Query(params);
        List<Map<String,Object>> returnList = new ArrayList<>();
	    if(params.containsKey("name")&&StringUtils.isNotBlank(params.get("name").toString())){
            List<MemberApplyDO> memberApplyList = memberApplyService.likeList(query);
            if(memberApplyList.size()>0){
				for (MemberApplyDO memberApplyDO : memberApplyList) {
					MemberUserDO memberUserDO = memberUserDao.get(memberApplyDO.getUserId());
					Map<String, Object> stringObjectMap = BeanUtil.beanToMap(memberApplyDO);
					stringObjectMap.put("userName",memberUserDO.getName());
					stringObjectMap.put("phone",memberUserDO.getPhone());
					stringObjectMap.put("avator",memberUserDO.getAvator());
					stringObjectMap.put("leve",memberUserDO.getLevel());
					stringObjectMap.put("userId",memberUserDO.getId());
					int teamNum = memberUserDao.getTeamNum(stringObjectMap);
					stringObjectMap.put("ztNum",teamNum);
					dateMap.put("userId",memberUserDO.getId());
					BigDecimal bigDecimal = orderTeamMoneyDao.sumMoneyByUserId(dateMap);
					stringObjectMap.put("teamMoney",bigDecimal);
					returnList.add(stringObjectMap);
				}
			}
            int total = memberApplyList.size();
            PageUtils pageUtils = new PageUtils(returnList, total);
            return pageUtils;
		}

		List<MemberApplyDO> memberApplyList = memberApplyService.list(query);
		if(memberApplyList.size()>0){
			for (MemberApplyDO memberApplyDO : memberApplyList) {
				MemberUserDO memberUserDO = memberUserDao.get(memberApplyDO.getUserId());
				Map<String, Object> stringObjectMap = BeanUtil.beanToMap(memberApplyDO);
				stringObjectMap.put("userName",memberUserDO.getName());
				stringObjectMap.put("phone",memberUserDO.getPhone());
				stringObjectMap.put("avator",memberUserDO.getAvator());
				stringObjectMap.put("leve",memberUserDO.getLevel());
				stringObjectMap.put("userId",memberUserDO.getId());
				int teamNum = memberUserDao.getTeamNum(stringObjectMap);
				stringObjectMap.put("ztNum",teamNum);
				dateMap.put("userId",memberUserDO.getId());
				BigDecimal bigDecimal = orderTeamMoneyDao.sumMoneyByUserId(dateMap);
				stringObjectMap.put("teamMoney",bigDecimal);
				returnList.add(stringObjectMap);
			}
		}
		int total = memberApplyService.count(query);
		PageUtils pageUtils = new PageUtils(returnList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:memberApply:add")
	String add(){
	    return "wx/memberApply/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:memberApply:edit")
	String edit(@PathVariable("id") String id,Model model){
		MemberApplyDO memberApply = memberApplyService.get(id);
		model.addAttribute("memberApply", memberApply);
	    return "wx/memberApply/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:memberApply:add")
	public R save( MemberApplyDO memberApply){
	    memberApply.setId(UuidUtil.get32UUID());
	    memberApply.setDelFlag(0);
	    memberApply.setCaAt(new Date());
		if(memberApplyService.save(memberApply)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:memberApply:edit")
	public R update( MemberApplyDO memberApply){
		memberApply.setUpAt(new Date());
		memberApplyService.update(memberApply);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:memberApply:remove")
	public R remove( String id){
		MemberApplyDO memberApply = memberApplyService.get(id);
		memberApply.setDelFlag(1);
		memberApply.setUpAt(new Date());
		if(memberApplyService.update(memberApply)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:memberApply:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				MemberApplyDO memberApply = memberApplyService.get(string);
				memberApply.setDelFlag(1);
				memberApply.setUpAt(new Date());
				memberApplyService.update(memberApply);
			}
		}
	
		return R.ok();
	}

	/**
	 * 审核
	 */
	@ResponseBody
	@PostMapping("/shen")
	@RequiresPermissions("wx:memberApply:edit")
	public R shen(MemberApplyDO memberApplyDO){
		return memberApplyService.shen(memberApplyDO);
	}

}
